package com.xhwl.logistics.service.logistics.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.logistics.converter.mapstruct.logistics.SecurityRiskConverter;
import com.xhwl.logistics.dao.mapper.logistics.SecurityRiskMapper;
import com.xhwl.logistics.entity.logistics.SecurityRisk;
import com.xhwl.logistics.logistics.request.SecurityRiskParam;
import com.xhwl.logistics.service.logistics.SecurityRiskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

/**
 * <p>
 * 安全隐患上报 服务实现类
 * </p>
 *
 * @author lin.yang
 * @since 2023-12-15
 */
@Service
@Slf4j
public class SecurityRiskServiceImpl extends ServiceImpl<SecurityRiskMapper, SecurityRisk> implements SecurityRiskService {

    @Autowired
    private SecurityRiskMapper securityRiskMapper;

    @Override
    public ResultJson saveOrUpdateSecurityRisk(SecurityRiskParam param) {
        log.info("saveOrUpdateSecurityRisk() called with parameters => 【param = {}】", param);
        try {
            SecurityRisk po = SecurityRiskConverter.INSTANCE.paramToPoConvert(param);
            if (ObjectUtil.isNull(Optional.ofNullable(po).map(x -> x.getCreateTime()).orElse(null))) {
                po.setCreateTime(new Date());
            }
            this.saveOrUpdate(po);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultJson.fail("上报失败");
        }
        return ResultJson.success();
    }
}
